extends Node2D

@export var item:Item
@onready var timer=$Timer
@onready var animation=$AnimatedSprite2D
@onready var apples=preload('res://地图/环境/苹果/苹果.tscn')
@onready var mark=$Marker2D

const STATE=['无苹果','苹果'] 
var state=STATE[1]
var player:Player

# 初始化
func _ready() -> void:
	startGrow()

func _physics_process(_delta: float) -> void:
	if state==STATE[1] and player and Input.is_action_just_pressed('交互'):pickApple()
	if animation.animation==state:return
	animation.play(state)


# 苹果开始生长
func startGrow() -> void:
	state=STATE[0]
	timer.start()

# 采摘苹果
func pickApple():
	startGrow()
	var apple=apples.instantiate()
	apple.global_position=mark.global_position
	get_parent().add_child(apple)
	player.collect(item)

func _on_pick_area_body_entered(body:Node2D) -> void:
	if body is Player:player=body

func _on_pick_area_body_exited(body:Node2D) -> void:
	if body is Player:player=null

# 苹果生成完成
func _on_timer_timeout() -> void:
	if state==STATE[0]:state=STATE[1]
